package com.canbot.u05.activity.customvoice.tool

import android.app.Dialog
import android.content.Context
import android.graphics.Color
import android.graphics.Point
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.support.v7.widget.GridLayoutManager
import android.support.v7.widget.RecyclerView
import android.text.TextUtils
import android.view.View

import com.canbot.u05.R
import com.canbot.u05.activity.customvoice.adapter.CustomDialogAudioAdapter
import com.canbot.u05.activity.customvoice.bean.CustomAudioData
import com.canbot.u05.activity.loge
import com.canbot.u05.socket.MsgSendUtils
import com.canbot.u05.socket.MsgType
import com.canbot.u05.utils.ToastTools
import kotlinx.android.synthetic.main.custom_dialog_audio_view.*

/**
 * CustomDialogAudioView
 *
 * @author Liu.Heng
 * @date 2019/6/3
 */
class CustomDialogAudioView(private val mContext: Context, private val mCustomAudioDataList: List<CustomAudioData>, private var voice: String?, private val listener: OnCloseListener?, private val position: Int) : Dialog(mContext) {


    private val className = "CustomDialogAudioView"

    private lateinit var mMyAdapter: CustomDialogAudioAdapter

    interface OnCloseListener {

        /***
         * 点击按钮回调
         * @param dialog  mContext
         * @param confirm true or false
         * @param audioPath 音频路径
         */
        fun onClick(dialog: Dialog, confirm: Boolean, audioPath: String?)

        /***
         * 点击按钮回调
         * @param dialog  mContext
         * @param position 下标
         * @param audioPath 音频路径
         */
        fun onClickPosition(dialog: Dialog, position: Int, audioPath: String?)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.custom_dialog_audio_view)
        setCanceledOnTouchOutside(false)
        /**设置弹窗的宽度*/
        val m = window?.windowManager
        val d = m?.defaultDisplay
        val p = window?.attributes
        val size = Point()
        d?.getSize(size)
        p?.width = size.x
        p?.height = size.y
        window?.attributes = p
        window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
        hideBottomUIMenu()
        initView()
    }

    private fun initView() {
        cancel_btn.setOnClickListener(::onClick)
        affirm_btn.setOnClickListener(::onClick)
        val recyclerView = findViewById<RecyclerView>(R.id.my_recyclerView)
        /**网格布局，每行为5*/
        recyclerView.layoutManager = GridLayoutManager(mContext, 5)
        recyclerView.addItemDecoration(GridSpacingItemDecoration(5, mContext.resources.getDimension(R.dimen.px56).toInt(), false))
        mMyAdapter = CustomDialogAudioAdapter(mContext, mCustomAudioDataList, object : CustomDialogAudioAdapter.OnItemClickListener {
            override fun onItemClick(view: View) {
                val i = view.tag as Int
                mMyAdapter.setChecked(i)
                mMyAdapter.notifyDataSetChanged()
                voice = mCustomAudioDataList[i].path
                val isSuccess = MsgSendUtils.sendStringMsg(MsgType.PLAY_SOUND, voice)
                loge("$className 播放路径==$voice")
                if (!isSuccess) {
                    ToastTools.newInstance(mContext.applicationContext).showText(mContext.getString(R.string.network_error))
                }
            }
        })
        recyclerView.adapter = mMyAdapter
        if (!TextUtils.isEmpty(voice)) {
            for (i in mCustomAudioDataList.indices) {
                if (voice == mCustomAudioDataList[i].path) {
                    mMyAdapter.setChecked(i)
                }
            }
            mMyAdapter.notifyDataSetChanged()
        }
    }

    private fun onClick(view: View) {
        when (view.id) {
            R.id.cancel_btn -> this.dismiss()
            R.id.affirm_btn -> if (listener != null) {
                if (position == -1) {
                    listener.onClick(this, true, voice)
                } else {
                    listener.onClickPosition(this, position, voice)
                }
            }
            else -> {
            }
        }
    }

    /**
     * 隐藏虚拟按键，并且全屏
     */
    private fun hideBottomUIMenu() {
        val attributes = window?.attributes
        attributes?.systemUiVisibility = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
        window?.attributes = attributes
        window?.decorView?.systemUiVisibility = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
        window?.decorView?.setOnSystemUiVisibilityChangeListener {
            var uiOptions = View.SYSTEM_UI_FLAG_LAYOUT_STABLE or
                    /**布局位于状态栏下方*/
                    View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION or
                    /**全屏*/
                    View.SYSTEM_UI_FLAG_FULLSCREEN or
                    /**隐藏导航栏*/
                    View.SYSTEM_UI_FLAG_HIDE_NAVIGATION or
                    View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
            uiOptions = uiOptions or 0x00001000
            window?.decorView?.systemUiVisibility = uiOptions
        }
    }
}

